class CategoriesController < ApplicationController
  def index
    @categories = Category.all
  end

  def new
    @category = Category.new
  end

  def edit
    @category = Category.find(params[:id])
  end

  def create
    @category = Category.new(params[:category])
    if @category.save
      redirect_to(categories_path, :success => 'Category was successfully created.')
    else
      render(:action => "new")
    end
  end

  def update
    @category = Category.find(params[:id])

    if @category.update_attributes(params[:category])
      redirect_to(categories_path, :success => 'Category was successfully updated.')
    else
      render(:action => "edit")
    end
  end

  def destroy
    @category = Category.find(params[:id])
    begin 
      @category.destroy 
    rescue
      flash[:error] = $!.to_s
    end
    redirect_to(categories_path)
  end
end
